package com.cl.it.spider;

import com.cl.it.mbg.entity.Company;
import com.cl.it.mbg.entity.News;
import com.cl.it.utils.DateUtils;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Component
@Slf4j
public class WebSiteSpider extends AbstractSpider {

    protected void executeSingleCompany(WebDriver driver, WebDriverWait wait, Company company) {
        convertlab(driver, wait, company);
        log.info("step log => convertlab提取成功 => {}！", company.getName());

        shence(driver, wait, company);
        log.info("step log => 神策提取成功 => {}！", company.getName());
    }

    @Override
    protected void login(WebDriver driver, WebDriverWait wait) {}

    private void shence(WebDriver driver, WebDriverWait wait, Company company) {
        if (!company.getName().contains("神策")) {
            return;
        }
        driver.get(company.getWebsite());
        List<WebElement> articles = selectors(driver, "ul.list li");
        if (CollectionUtils.isEmpty(articles)) {
            return;
        }
        for (int i = 0; i < articles.size(); i++) {
            WebElement element = articles.get(i);
            News news = new News();
            news.setDataSource("官网");
            news.setTitle(selectorText(element,"div.post-desc :nth-child(1)"));
            news.setSourceUrl(selector(element, "div.post-desc :nth-child(1)").getAttribute("href"));
            news.setContent(selectorText(element, "div.post-desc :nth-child(2)"));
            news.setNewsDate(DateUtils.dateConvert(selector(element, "div.post-meta time").getAttribute("datetime")));
            news.setName(company.getName());

            UpdateWrapper<News> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("title", news.getTitle());
            updateWrapper.eq("name", news.getName());
            newsService.saveOrUpdate(news, updateWrapper);
        }
    }

    private void convertlab(WebDriver driver, WebDriverWait wait, Company company) {
        if (!company.getName().contains("欣兆阳")) {
            return;
        }
        driver.get(company.getWebsite());
        List<WebElement> articles = selectors(driver, "section.resources>div.container-xl>div.article div.articleDesc");
        if (CollectionUtils.isEmpty(articles)) {
            return;
        }
        for (int i = 0; i < articles.size(); i++) {
            WebElement element = articles.get(i);
            News news = new News();
            news.setDataSource("官网");
            news.setTitle(selectorText(element,"div.content>div.title"));
            news.setSourceUrl(selector(element, "div.content>div.title>a").getAttribute("href"));
            news.setContent(selectorText(element, "div.content>div.description"));
            news.setNewsDate(DateUtils.dateConvert(selectorText(element, "div.bottomSection>div.left>div.date")));
            news.setName(company.getName());
            news.setAuthor(selectorText(element, "div.bottomSection>div.left>div.author"));

            UpdateWrapper<News> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("title", news.getTitle());
            updateWrapper.eq("name", news.getName());
            newsService.saveOrUpdate(news, updateWrapper);
        }
    }
}
